<?php
declare(strict_types=1);

namespace Modules\Inspection\Http\Controllers;

use Catch\Base\CatchController as Controller;
use Modules\Inspection\Models\Inspectioncheck;
use Modules\Inspection\Models\Inspection;
use Illuminate\Http\Request;
use Modules\Inspection\Http\Repositories\InspectionReget;
use Catch\Exceptions\FailedException;
use Illuminate\Support\Facades\DB;
use extend\services\warehouse\{warehouseService};//引入自定义类

class InspectionController extends Controller
{
    public function __construct(
        protected readonly Inspection $model
    ){}

    /**
     * @param Request $request
     * @return mixed
     */
    public function index(Request $request): mixed
    {
   
        $data =InspectionReget::list($request->all());
        $kingDeeService = new warehouseService();
        $datauser = $kingDeeService->uerdata();
        foreach($data as $k=>$v){
            $data[$k]['creator_id'] = isset($datauser[$v['creator_id']])?$datauser[$v['creator_id']]['username']:'';

            $fnumber = DB::table('material')
            ->select('id','fnumber','fname','fspecification','funit_number','funit_name')
            ->whereIn('id',$v['productname'])
            ->first();

            $name ='';
            $fname = '';
            if(!empty($fnumber)){
                $name .= $fnumber->fnumber.'、';
                $fname .= $fnumber->fname.'、';
                $data[$k]['productname'] = rtrim($name,'、');
                $data[$k]['fname'] = rtrim($fname,'、');
            }else{
                $data[$k]['productname'] = '共用方案';
            }

        }
      
        return $data;
    }

    /**
     * @param Request $request
     * @return mixed
     */
    public function store(Request $request)
    {
        $data = $request->all();
        $dd = implode('',$data['productname']);
        $res = $this->model->where('productname', 'like', "%{$dd}%")->first();
        $data['creator_id'] =$this->getLoginUserId();//当前登录用户名称
         if(!empty($res)){
            throw new FailedException('产品名称重新请重新录入');
         }else{
            return InspectionReget::create($data);
         }
    }

    /**
     * @param $id
     * @return mixed
     */
    public function show($id)
    {
        return InspectionReget::find($id);
    }

    /**
     * @param Request $request
     * @param $id
     * @return mixed
     */
    public function update($id, Request $request)
    {   
        return InspectionReget::createUpdate($id,$request->all());
    }

    /**
     * @param $id
     * @return mixed
     */
    public function destroy($id,Request $request)
    {
        // return $this->model->deleteBy($id);
        return InspectionReget::delete($id,$request->all());
    }

    /**物料接口
     * @param $id
     * @return mixed
     */
    public function materiel()
    {
        //return $this->model->deleteBy($id);
        $data = DB::table('material')->select('id','fnumber','fname','fspecification','funit_number','funit_name')->limit(1)->get()->toArray();
        return ['code'=>1000,'message'=>'请求成功','data'=>$data];;
    }



}
